/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.powertalk3.server.util;

import cn.powertalk3.server.protocol.DefaultProtocol;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.mina.common.DefaultIoFilterChainBuilder;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.common.IoAcceptorConfig;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;

/**
 *
 * @author mrgaolei
 */
public class SocketUtil {

    private static final int PORT = 10123;
    private static final IoAcceptor acceptor;

    static {
        acceptor = new SocketAcceptor();
        IoAcceptorConfig config = new SocketAcceptorConfig();
        DefaultIoFilterChainBuilder chain = config.getFilterChain();
        chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
        try {
            acceptor.bind(new InetSocketAddress(PORT), new DefaultProtocol(), config);
        } catch (IOException ex) {
            Logger.getLogger(SocketUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Server start at port : " + PORT);
    }

    public static IoAcceptor getIoAcceptor() {
        return acceptor;
    }

}
